package metuan_;

import java.util.Arrays;

public class ArrayStack {
    private int top = -1;//记住初始为-1，没有元素，有元素时始终指向栈顶元素
    private int maxSize;
    private int[] stack;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("is empty");
        }
        int value = stack[top--];
        System.out.println("current top element is " + value);
        return value;
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("is full");
            reSize();
        }
        stack[++top] = value;
    }

    public void reSize() {
        int oldSize = maxSize;
        maxSize = oldSize << 1;
        stack = Arrays.copyOf(stack, maxSize);
    }
}


